<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="closeFn">
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="formData.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment">
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" @focus="showDepts=true" />
        <!--
            data 传入渲染的数据数组
            props 修改渲染的属性名
            default-expand-all 展开所有节点
         -->
        <el-tree v-show="showDepts" :data="depts" :props="{label:'name'}" default-expand-all @node-click="checkDepts" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="confirmFn">确定</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>
<script>
import employees from '@/api/constant/employees'
import { getDepartments } from '@/api/departments'
import { addEmployees } from '@/api/employees'
import { listToTree } from '@/utils/index'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '请输入工号', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      },
      hireType: employees.hireType, // 聘用形式 列表
      depts: [],
      showDepts: false // 控制部门列表的显示与隐藏
    }
  },
  async created() {
    // 获取部门列表
    const res = await getDepartments()
    // console.log(res)
    this.depts = listToTree(res.depts, '')
  },
  methods: {
    // 确定按钮 / 新增
    async confirmFn() {
      // 校验规则
      await this.$refs.form.validate()
      //   调用接口
      await addEmployees(this.formData)
      // 关闭弹窗
      this.closeFn()
      // 刷新列表
      this.$emit('updateEvent')
    },
    // 封装关闭弹窗方法
    closeFn() {
      // 关闭弹窗
      this.$emit('update:showDialog', false)
      // 清空数据
      this.formData = {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }
      //   重置表单
      this.$refs.form.resetFields()
    },
    // 点击部门的时候触发
    checkDepts(obj) {
    //   console.log(obj)
      // 渲染选中的部门
      this.formData.departmentName = obj.name
      this.showDepts = false
    }
  }
}
</script>
<style scoped>
.el-form-item{
    position: relative;
}
.el-tree{
    position: absolute;
    z-index: 1;
    width: 100%;
    height: 150px;
    overflow: auto;
}
</style>
